package netty;

import org.springframework.beans.BeansException;
import org.springframework.cglib.reflect.FastClass;
import org.springframework.cglib.reflect.FastMethod;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;

/**
 * @Author: dch
 * @Date: 2020/6/7 10:57
 * @Description:
 */
public class nettyTestReflection {
    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException {


        //封装
        RpcRequest request = new RpcRequest();
        String requestId = UUID.randomUUID().toString();
        System.out.println(requestId);

        Class clazz = Class.forName("netty.UserService");
        System.out.println("【Class】"+clazz);

        Method method=clazz.getMethod("sayHello",Object.class);
        String className = method.getDeclaringClass().getName();
        System.out.println(className);
        String methodName = method.getName();
        System.out.println(methodName);

        Class<?>[] parameterTypes = method.getParameterTypes();
        System.out.println(parameterTypes);


        //使用Class.forName进行加载Class文件
        Class<?> clazz2 = Class.forName(request.getClassName());
        Object serviceBean =null;// applicationContext2.getBean(clazz2);

        Class<?> serviceClass = serviceBean.getClass();

        String methodName1 = request.getMethodName();

        Class<?>[] parameterTypes1 = request.getParameterTypes();
        Object[] parameters = request.getParameters();

        //使用CGLB Reflect
        FastClass fastClass = FastClass.create(serviceClass);
        FastMethod fastMethod = fastClass.getMethod(methodName1, parameterTypes1);
        fastMethod.invoke(serviceBean, parameters);

    }
}
